package com.flowerSystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flowerSystem.entity.Productinformation;
import com.flowerSystem.pojo.*;
import com.flowerSystem.service.ProductInformationService;
import com.flowerSystem.util.PageResult;
import com.flowerSystem.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/manager")
@CrossOrigin
@Slf4j
public class ProductInformationController {

    @Resource
    private ProductInformationService productInformationService;

    /**
     * 产品添加(添加到数据库)
     * @param productinformation
     * @return
     */
    @PostMapping("/productInform")
    public ResponseResult productInformAdd(@RequestBody Productinformation productinformation){
//        System.out.println("productinformation = " + productinformation);
        if (productinformation != null){
            boolean save = productInformationService.save(productinformation);
            if(save){
                return ResponseResult.ok(productinformation);
            }
        }
        return ResponseResult.fail();
    }

    /**
     * 根据id删除产品
     * @param
     * @return
     */
    @DeleteMapping("/productInform/{id}")
    public ResponseResult productInformDel(@PathVariable("id") int id){
        boolean b = productInformationService.removeById(id);
        if(b){
            return ResponseResult.ok(null);
        }
        return ResponseResult.fail();
    }

    /**
     * 根据id修改产品信息
     * @return
     */
    @PutMapping("/productInform")
    public ResponseResult productInformUpdate(@RequestBody Productinformation productinformation){
        if(productinformation != null){
            boolean update = productInformationService.updatProductById(productinformation);
            if(update){
                return ResponseResult.ok(productinformation);
            }
        }
        return ResponseResult.fail();
    }

    /**
     * 获取产品信息表所有信息
     * 分页查询
     * @return
     */
//    @GetMapping("/productInform/{pageNum}/{pageSize}")
////    @GetMapping("/productInform")
//    public ResponseResult productInformSelectAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
    @PostMapping("/productInformGet")
//    @GetMapping("/productInform")
    public ResponseResult productInformSelectAll(@RequestBody FenYe fenYe){
//    public ResponseResult productInformSelectAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        //指定页码和页容
//        FenYe<Productinformation> page = new FenYe<>(pageNum, pageSize);
        //对所有数据分页
//        page = productinformationService.page(page);
//-------------------------------------------------------------------------------------------------
//        //按条件分页(对所有age>=20用户进行分页查询)
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.ge("age",20);
//        page = productinformationService.page(page,queryWrapper);
//-------------------------------------------------------------------------------------------------
        //获取当前页数据
//        List<Productinformation> userList = page.getRecords();
//        userList.forEach(productinformation -> System.out.println(productinformation));
//        //获取总记录数
//        System.out.println("总记录数:"+page.getTotal());
//        System.out.println("总页数:"+page.getPages());
//-------------------------------------------------------------------------------------------------
//        Page<Productinformation> page = new Page<>(fenYe.getPageNum(), fenYe.getPageSize());
//        System.out.println("页容"+fenYe.getPageNum());
//        System.out.println("页码"+fenYe.getPageSize());
//        Page<Productinformation> page = new Page<>(fenYe.getPageNum(), fenYe.getPageSize());
//        productInformationService.page(page);
//        return ResponseResult.ok(page);

        PageResult pageResult = productInformationService.productInformSelectAll(fenYe.getPageNum(), fenYe.getPageSize());
        if(pageResult != null && pageResult.getTotal() > 0){
            return ResponseResult.ok(pageResult);
        }
        return ResponseResult.fail();
    }

    /**
     * 管理员动态sql模糊分页查询商品信息
     * @param
     * @return
     */
//    @GetMapping("/productInform/{pageNum}/{pageSize}/{flowerName}/{classify}")
    @PostMapping("/productInforms")
    public ResponseResult productInformByNameType(@RequestBody ProductinformVo productinformVo){
        System.out.println("ProductinformVo = " + productinformVo);
//    public ResponseResult productInformByNameType(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @PathVariable("flowerName") String flowerName, @PathVariable("classify") String classify){
           PageResult pageResult = productInformationService.productInformByNameType(productinformVo);
           log.info("......cccccccc....");
           System.out.println(pageResult.getData());
        if(pageResult != null && pageResult.getTotal() > 0){
              return ResponseResult.ok(pageResult);
          }
        return ResponseResult.fail();
    }

    /**
     * 通过产品id，获取当前产品所有详情信息
     * @param id
     * @return
     */
    @GetMapping("/productInformSingle")
    public ResponseResult productInformSingle(@RequestParam int id){
        ProductinformationVo collection = productInformationService.productInformSingle(id);
        if(collection != null){
            return ResponseResult.ok(collection);
        }
        return ResponseResult.fail();
    }

}
